<template>
    <a-modal
            :title="title"
            :width="width"
            :visible="visible"
            switchFullscreen
            @cancel="handleCancel"
            class="printData"
    >
        <template slot="footer">
            <a-button key="back" @click="handleCancel">
                关闭
            </a-button>
        </template>
        <div v-if="visible">
            <a-table

                    ref="table"
                    size="middle"
                    bordered
                    rowKey="id"
                    :columns="columns"
                    :dataSource="dataSource"
                    :pagination="ipagination"
                    :loading="loading"
                    class="j-table-force-nowrap"
                    @change="handleTableChange"
            >
                <template slot="htmlSlot" slot-scope="text">
                    <div v-html="text"></div>
                </template>
            </a-table>
        </div>
    </a-modal>
</template>

<script>
    /**
     * create by Jane
     */
    import '@/assets/less/TableExpand.less'
    import { JeecgListMixin } from '@/mixins/JeecgListMixin'
    import waste from '@/api/waste'

    export default {
        mixins: [JeecgListMixin],
        data() {
            return {
                title: '废物收运记录',
                width: '80%',
                visible: false,
                // 表头
                columns: [],
                url: {
                    list: null
                },

                wasteId: [],
                wasteTypeList: [],
                wasteType: []
            }
        },
        methods: {
            async show(info) {
                if (info && info.storage) {
                    this.visible = true
                    this.url.list = '/hc/hcStorage/queryStoragePageList';
                    this.queryParam.storageNumber = info.storage
                    await this.queryWasteList()
                    this.loadData(1)
                }
            },
            // 查询废物列表
            async queryWasteList() {
                let data = await waste.getWasteTypeListByCompanyId()
                let arr = []
                let wasteIds = []
                data.forEach((item, index) => {
                    (item['children'] || []).forEach((val, i) => {
                        if (index == 0) {
                            arr.push({
                                value: val.id,
                                label: val.wasteTypeName,
                                checked: true
                            })
                            wasteIds.push(val.id)
                        } else {
                            arr.push({
                                value: val.id,
                                label: val.wasteTypeName,
                                checked: false
                            })
                        }
                    })
                })
                this.wasteId = wasteIds
                this.wasteTypeList = data
                this.wasteType = arr

                this.setColumns()
            },

            setColumns() {
                let cols = []

                let data = this.wasteTypeList
                data.forEach((item, index) => {
                    if (item.wasteTypeName) {
                        let child = [];
                        (item['children'] || []).forEach((val, i) => {
                            let info = this.wasteId.find(v => v === val.id)
                            if (info) {
                                child.push({
                                    title: `${val.wasteTypeName}${val.priceUnit ? `(${val.priceUnit})` : ''}`,
                                    dataIndex: 'id',
                                    align: 'center',
                                    key: `${index}-${i}`,
                                    customRender: function(vv, waste, index) {
                                        let info = (waste['list']).find((v) => v.wasteId === val.id)
                                        if (info) {
                                            const obj = {
                                                children: < div
                                            style = 'font-weight:600;' > { info.wastePresentValue || 0 }
                                            {
                                                val.priceUnit || ''
                                            }
                                        <
                                            /div>,
                                            attrs: {
                                            }
                                        }
                                            return obj
                                        }
                                        return ' '
                                    }
                                })
                            }
                        })
                        if (child.length) {
                            let obj = {
                                title: item.wasteTypeName,
                                dataIndex: 'id',
                                key: `${index}`,
                                children: child
                            }
                            cols.push(obj)
                        }
                    }
                })

                this.columns = [
                    {
                        title: '序号',
                        dataIndex: '',
                        key: 'rowIndex',
                        width: 60,
                        align: 'center',
                        customRender: function(t, r, index) {
                            return parseInt(index) + 1
                        }
                    },
                    {
                        title: '收运编号',
                        align: 'center',
                        dataIndex: 'recycleNumber',
                        customRender: function(value, row, index) {
                            return value || ' '
                        }
                    },
                    {
                        title: '产生单位',
                        align: 'center',
                        dataIndex: 'customerName'
                    },
                    ...cols,
                    {
                        title: '收运编号',
                        align: 'center',
                        dataIndex: 'recycleNumber'
                    },
                    {
                        title: '收运人',
                        align: 'center',
                        dataIndex: 'recyclePersonName'
                    },
                    {
                        title: '收运时间',
                        align: 'center',
                        dataIndex: 'createTime'
                    },
                    {
                        title: '运输车辆',
                        align: 'center',
                        dataIndex: 'carNumber'
                    },
                    {
                        title: '交接编号',
                        align: 'center',
                        dataIndex: 'transferNumber'
                    },
                    {
                        title: '交接人',
                        align: 'center',
                        dataIndex: 'transferName'
                    },
                    {
                        title: '交接时间',
                        align: 'center',
                        dataIndex: 'transferTime'
                    },
                    {
                        title: '入库编号',
                        align: 'center',
                        dataIndex: 'storageNumber'
                    },
                    {
                        title: '入库人',
                        align: 'center',
                        dataIndex: 'storagePersonName'
                    },
                    {
                        title: '入库时间',
                        align: 'center',
                        dataIndex: 'storageTime'
                    },
                    {
                        title: '备注',
                        align: 'center',
                        dataIndex: 'remarks'
                    }
                ]
            },
            close() {
                this.$emit('close')
                this.visible = false
            },
            handleOk() {
                this.close()
            },
            handleCancel() {
                this.close()
            },
            handleEdit() {

            }
        }
    }
</script>
<style scoped>
    @import '~@assets/less/common.less';
</style>
